//
//  main.m
//  省市区
//
//  Created by Hanlingyu on 16/7/9.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "City.h"
#import "Province.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    
#pragma mark - 第四题
#if 1
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/ltq/DLS160710/Objective-C/HomeWork/省市区/省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", sourceString);
    // 根据回车, 分割字符串
    
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    
    // 存放所有省的数组
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        // 取到所有的省(没有空格开头)
        if (![area hasPrefix:@" "]) {
            // 创建了省字典(字典中包含连个字段 name cityArray)
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 添加proName字段
            [proDic setObject:area forKey:@"proName"];
            // 创建了省中所有城市的数组
            NSMutableArray *cityArray = [NSMutableArray array];
            // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            // 创建好的省字典, 放到省数组中
            [allProvinceArray addObject:proDic];
        // 取到所有的市(两个空格开头, 但不是四个空格开头)
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            // 难点: 通过lastObject获取到对应的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 拿到省字典中对应的市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 创建一个市字典(name, zoneArray)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            // 向市数组中添加市字典
            [cityArray addObject:cityDic];
            
        } else {
            // 通过省数组获取到当前所在省的字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 通过市数组获取到对应的市字典
            NSMutableDictionary *city = [cityArray lastObject];
            // 通过市字典渠道对应的区/县的数组
            NSMutableArray *zoneArray = [city objectForKey:@"zoneArray"];
            // 最终将区/县添加到数组中
            [zoneArray addObject:area];
            
        }
        
    }
    
    
    // 省数组 - Province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if (![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    
    NSLog(@"%@", allProArray);
#endif
    
#pragma mark - 第五题
#if 0
    NSString *locationPath = @"/Users/ltq/DLS160710/Objective-C/HomeWork/省市区/省市区/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
    // 根据",", 分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
//    NSLog(@"%@", allCityArray);
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的"|"处理字符串, 重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据"|"切割生成的数组, 生成新的城市字典
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        // 添加到新的数组中
        [allCityDicArray addObject:cityDic];
    }
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
            
            
        } else if ([cityNumber hasSuffix:@"00"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        
        
        } else {
//            NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
    
    NSLog(@"%@", finalArray);
    
    
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据"|"切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithCityName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
//    NSLog(@"%@", allTempCityArray);
    
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [pro.array addObject:city];
            
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        
        }
        
        
    }
    NSLog(@"%@", finalClassArray);
#endif
    return 0;
}
